package com.cogwheel.framework.graphics.ui;

import javax.media.opengl.GL2;

import com.cogwheel.framework.graphics.CSprite;
import com.cogwheel.framework.init.CPreferences;
import com.cogwheel.framework.util.CAction;
import com.cogwheel.framework.util.CAssets;
import com.cogwheel.framework.util.CRectangle;
import com.cogwheel.framework.util.CUtils;
import com.cogwheel.framework.util.input.CKeyboard;
import com.cogwheel.framework.util.input.CMouse;
import com.cogwheel.framework.util.input.CMouseState;

public class CButton extends CSprite{
	public CFont Text = null;
	public String TextureID2 = null;
	public boolean Hovered = false;
	
	private CAction action;
	
	public CButton(CPreferences preferences, String spriteID, String spriteIDHover, CRectangle dimensions, CAction action){
		this(preferences, spriteID, spriteIDHover, dimensions, null, 0f, 0f, action);
	}
	public CButton(CPreferences preferences, String spriteID, String spriteIDHover, CRectangle dimensions, String text, float textScale, float xoffset, CAction action) {
		super(preferences, spriteID, dimensions);
		this.TextureID2 = spriteIDHover;
		this.action = action;
	}
	
	@Override
	public void CDraw(GL2 gl){
		CBindTexture(gl);
		CToOrtho();
		CDraw2D(gl);
		if (Text != null) this.Text.CDraw(gl);
	}
	
	
	@Override
	public void CBindTexture(GL2 gl){
		if (Hovered){
			if (TextureID2 != null && CUtils.CurrentTexture != TextureID2){
				CAssets.CWGGetTexture(TextureID2).enable(gl);
				CAssets.CWGGetTexture(TextureID2).bind(gl);
				
				CUtils.CurrentTexture = TextureID2;
			}
		}else{
			if (TextureID != null && CUtils.CurrentTexture != TextureID){
				CAssets.CWGGetTexture(TextureID).enable(gl);
				CAssets.CWGGetTexture(TextureID).bind(gl);
				
				CUtils.CurrentTexture = TextureID;
			}
		}
	}
	
	@Override
	public void CUpdate(CMouse mouse, CKeyboard keyboard){
		Hovered = this.Dimensions.Intersects(new CRectangle(mouse.CWGGetMouseCoords(), 1, 1));
		if (Hovered && mouse.CWGGetMouseState() == CMouseState.RIGHT_CLICK){ if (action != null) action.CWGPerform(); mouse.CWGSetMouseState(CMouseState.NOTHING); }
	}
	
	public void CUpdate(CMouse mouse){
		this.CUpdate(mouse, null);
	}
	
}
